package chap10

abstract class Justify {
    def justify(element : Element, w : Int): Element
}

object Justify {
  
  case class Left extends Justify {
    override def justify(element : Element, w : Int) = { 
      val right = Element(' ', w - element.width, element.height, element.justify)
      element beside right
    }
  }
  case class Center extends Justify {
    override def justify(element : Element, w : Int) = { 
      val left = Element(' ', (w - element.width) / 2, element.height, element.justify)
      val right = Element(' ', w - element.width - left.width, element.height, element.justify)
      left beside element beside right
    }
  }
  case class Right extends Justify {
    override def justify(element : Element, w : Int) = { 
      val left = Element(' ', w - element.width, element.height, element.justify)
      left beside element
    }
  }
  
  def apply(str : String): Justify = {
    str match {
      case "right" => new Right
      case "center" => new Center
      case "left" => new Left
      case _ => new Center
    }
  }
}